'''
Created on Nov 11, 2011

@author: Victor Bos
'''
import copy
import sys
import io

######################################################################
class UnitType:

    def __init__(self, name, attrs):
        self.name = name
        self.attrs = attrs
        self.xbm = None
        
    def __cmp__(self, other):
        return cmp(self.name, other.name)

    def __hash__(self):
        return hash(self.name) ^ 7243 # 7243 is prime

    def __copy__(self):
        return UnitType(
            copy.copy(self.name),
            copy.copy(self.attrs))
    
    def debug_print_info(self):
        """
        Prints the stats a unit to stdout.
        """
        msg = [
            "Unit type: %s" % self.name
            ]
        for a in self.attrs:
            msg.append("%s: %s" % (a, self.attrs[a]))
        sys.stdout.write("\n".join(msg) + "\n\n")

    def get_save_str(self):
        result = []
        result.append("begin Unit %s\n" % self.name)
        result.append(self.get_attr_str("  ") + "\n")
        result.append("end Unit %s\n" % self.name)
        return "".join(result)

    def get_attr_str(self, spc=""):
        return io.attrs_to_str(self.attrs, spc=spc)
